<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:29
 */

class RetainService extends Service {

    public static function indexHeader($params){
        $header = array(
            'RecDate'   => array('text' => PubFunc::t('日期')),
            'SID'       => array('text' => PubFunc::t('服')),
            'RegUser'   => array('text' => '新用户数'),
        );

        $days = self::dayList();

        foreach($days as $v){
            $header["Remain{$v}Day"] = array('text' => $v.'日留存率', 'rule'=>'outHtml');
        }
        
        if($params['server'] == 'ALL'){
        	unset($header['SID']);
        }

        return $header;
    }

    public static function indexData($params){
        if (PubFunc::checkEmpty($params, array('g', 'server', 'time_start', 'time_end'))) return array();

        $data = ModRetain::indexData($params);
        
        $new_data = array();
        foreach ($data as $key=>$val){
        	if($params['server'] != 'ALL'){
        		$new_data[$val['RecDate']]['SID'] = $val['SID'];
        	}
        	$new_data[$val['RecDate']]['RecDate'] = $val['RecDate'];
        	$new_data[$val['RecDate']]['RegUser'] += $val['RegUser'];
        	$new_data[$val['RecDate']]['CumRegUser'] += $val['CumRegUser'];
        	$new_data[$val['RecDate']]['RoleUser'] += $val['RoleUser'];
        	$new_data[$val['RecDate']]['CumRoleUser'] += $val['CumRoleUser'];
        	
        	$new_data[$val['RecDate']]['Remain1Day'] += $val['Remain1Day'];
        	$new_data[$val['RecDate']]['Remain2Day'] += $val['Remain2Day'];
        	$new_data[$val['RecDate']]['Remain3Day'] += $val['Remain3Day'];
        	$new_data[$val['RecDate']]['Remain7Day'] += $val['Remain7Day'];
        	
        	$new_data[$val['RecDate']]['Remain14Day'] += $val['Remain14Day'];
        	$new_data[$val['RecDate']]['Remain21Day'] += $val['Remain21Day'];
        	$new_data[$val['RecDate']]['Remain30Day'] += $val['Remain30Day'];
        	$new_data[$val['RecDate']]['Remain60Day'] += $val['Remain60Day'];
        	$new_data[$val['RecDate']]['Remain90Day'] += $val['Remain90Day'];
        	$new_data[$val['RecDate']]['Remain4Day'] += $val['Remain4Day'];
        	$new_data[$val['RecDate']]['Remain5Day'] += $val['Remain5Day'];
        	$new_data[$val['RecDate']]['Remain6Day'] += $val['Remain6Day'];
        }
        

        $days = self::dayList();

        foreach($new_data as &$v){
            foreach($days as $i){
                if(empty($v['RegUser'])){
                    $v['RegUser'] = 0;
                }else{
                    $v["Remain{$i}Day"] = "{$v["Remain{$i}Day"]} <span style='color:green;'>(".number_format($v["Remain{$i}Day"] / $v['RegUser'] * 100, 2, '.', '')."%)</span>";
                }
            }
        }
        unset($v);

        return $new_data;
    }

    /**
     * 渠道用户留存 - 表头
     * @param $params
     * @return array
     */
    public static function channelHeader($params){
        $header = array(
            'RecDate'   => array('text' => '日期'),
            'SID'       => array('text' => '服'),
        );

        $channelList = self::channelList($params['g']);

        foreach($channelList as $k=>$v){
            $include = array("Value_{$k}" => array('text' => '用户数'));

            foreach($params['day'] as $dv){
                $include["Retain_{$k}_{$dv}"] = array('text' => "{$dv}日留存", 'rule'=>'outHtml');
            }

            $header[$k] = array('text' => $v, 'include' => $include);
        }

        return $header;
    }

    /**
     * 渠道用户留存 - 数据
     * @param $params
     * @return array
     */
    public static function channelData($params){
        if (PubFunc::checkEmpty($params, array('g', 'server', 'time_start', 'time_end'))) return array();

        $tmp = ModRetain::channelData($params);

        $dayList = self::dayList();
        $data = array();
        foreach($tmp as $v){
            $channelId = $v['CID'];

            if(!isset($data[$v['RecDate']])) {
                $data[$v['RecDate']]['RecDate'] = $v['RecDate'];
                $data[$v['RecDate']]['SID']     = $params['server'];
            }

            $data[$v['RecDate']]["Value_{$channelId}"] = $v['KPIValue'];
            foreach($dayList as $dv){
                if(isset($v["RemainDay{$dv}"])){
                    $data[$v['RecDate']]["Retain_{$channelId}_{$dv}"] = $v["RemainDay{$dv}"]." <span style='color:green;'>(".number_format($v["RemainDay{$dv}"] / $v['KPIValue'] * 100, 2) . '%)</span>';
                }
            }
        }

        return $data;
    }

    public static function channelList($game){
        $result = PubDict::dict($game, 'channel');
        return $result;
    }

    public static function dayList($key=false){
        $return = array(1,2,3,4,5,6,7,14,21,30,60,90);

        if($key){
            $result = array();
            foreach($return as $v){
                $result[$v] = $v.'日';
            }

            return $result;
        }else{
            return $return;
        }
    }

    /**
     * 渠道用户留存 - 表头
     * @param $params
     * @return array
     */
    public static function roleHeader($params){
        $header = array(
            'RecDate'   => array('text' => '日期'),
            'SID'       => array('text' => '服'),
        );

        $channelList = PubDict::dict('roma', 'roleocc');

        foreach($channelList as $k=>$v){
            $include = array("Value_{$k}" => array('text' => '活跃用户'));

            foreach($params['day'] as $dv){
                $include["Retain_{$k}_{$dv}"] = array('text' => "{$dv}日留存", 'rule'=>'outHtml');
            }

            $header[$k] = array('text' => $v, 'include' => $include);
        }

        return $header;
    }

    /**
     * 渠道用户留存 - 数据
     * @param $params
     * @return array
     */
    public static function roleData($params){
        if (PubFunc::checkEmpty($params, array('g', 'server', 'time_start', 'time_end'))) return array();

        $tmp = ModRetain::roleData($params);

        $dayList = self::dayList();
        $data = array();
        foreach($tmp as $v){
            $roleOcc = $v['RoleOcc'];

            if(!isset($data[$v['RecDate']])) {
                $data[$v['RecDate']]['RecDate'] = $v['RecDate'];
                $data[$v['RecDate']]['SID']     = $params['server'];
            }

            $data[$v['RecDate']]["Value_{$roleOcc}"] = $v['KPIValue'];
            foreach($dayList as $dv){
                if(isset($v["RemainDay{$dv}"])){
                    $data[$v['RecDate']]["Retain_{$roleOcc}_{$dv}"] = $v["RemainDay{$dv}"]." <span style='color:green;'>(".number_format($v["RemainDay{$dv}"] / $v['KPIValue'] * 100, 2) . '%)</span>';
                }
            }
        }

        return $data;
    }

} 